Storing Containers

Running container workloads on Kubernetes requires container images to be stored within a repository where the container orchestration platform can easily access them. A container registry is commonly used to store and retrieve container images in cloud-native scenarios. There are many container registries for developers to choose from such as Docker Hub and Azure Container Registry.

Container images are normally created as part of a team's CD pipeline when an automated CI system builds the container and stores it within a container registry. Once stored in the container registry, the container image can be pulled by other developers or platforms like Kubernetes.

Container registry setup#

For this course, we'll keep things simple by manually storing container images within a public Docker Hub registry. Before proceeding, we'll need to create a free container registry on Docker Hub.

To store container images within the Docker Hub container registry, we'll need to create a repository for the container images. To create the repository, click the "Create repository" button on the Docker Hub landing page.

The Docker Hub landing page
The Docker Hub landing page

Then, create a new public repository named python-sample.

Creating a repository on Docker Hub
Creating a repository on Docker Hub

Note: Remember the Docker Hub configuration including username, password, registry name, and repository name because this information will be necessary to complete the lessons in the course.

Pushing containers to Docker Hub#

Now, we'll store the container image created in the last lesson within the new Docker Hub repository. Once the image is placed in the registry, it will become available for the Kubernetes platform to pull and run on the cluster.

To begin, launch the interactive terminal, which contains the updated dockerfile from the previous lesson. Within the interactive terminal, we'll complete the following steps to push a new container image to the registry:

  1. Log in to DockerHub.

  2. Build and tag the image.

  3. Push the container image to Docker Hub.

  4. Retrieve the container image from Docker Hub.

Here's a visual sequence of the steps we'll be completing:

Login
Login
Push
Push
Build
Build
Retrieve
Retrieve
Viewer does not support full SVG 1.1
Steps for storing the container image

Log in to Docker Hub#

To interact with the registry on Docker Hub, we'll need to authenticate with the credentials we created earlier. Execute the following command within the terminal of the interactive console, which will prompt us to enter our Docker Hub credentials for authentication:

Command to authenticate with a Docker Hub container registry

After logging into Docker Hub via the command, we'll see the following output in the terminal of the interactive widget:

Output from the docker login command

Build and tag the image#

To store the image in the repository on Docker Hub, we'll first need to build the image locally with the name of our new Docker registry and repository. Fill in the placeholders and execute the following command within the terminal to build the image in this manner:

Command to build a container image

After executing the commands to build the image, the following output will appear in the terminal:

Output from the docker build command

Push the image#

Next, we'll push the container image from the local Docker registry to the registry we created on Docker Hub. Fill in the placeholders and execute the following command within the terminal to push the container image to Docker Hub:

Command to push a container image to a Docker Hub registry

After the command completes, we'll see the following output in the terminal of the interactive widget:

Output from the docker push command

We can return to the Docker Hub repository via a web browser to view the container image within the "Tags" tab.

The container image stored in Docker Hub
The container image stored in Docker Hub

Now, the newly built container image is available within the container registry where it can be retrieved by Docker and run as a container.

Retrieve the image#

Now that the image is stored in Docker Hub, let's remove it from the local registry and test pulling it from the remote registry. Execute the following commands within the terminal of the interactive widget, replacing the placeholders with the Docker Hub username:

Commands to remove the container image and pull it remotely from Docker Hub

When the docker rmi command is executed, the following output will display on the console:

Output from the docker rmi command

Then, we'll notice that the image has been removed from the local images by observing the output of the docker images command:

Output of the docker images command that indicates the container image is removed

Finally, when we execute the command to run the container on line 8, we'll notice that the container image is pulled to the local registry from Docker Hub and launched by Docker.

Output of the docker run command

This confirms that the container image is available to be pulled and run by any host running Docker. At this point, we could reference this container image in a declarative Kubernetes manifest to add the sample Python application within our system's desired state.

Test the application#

With the container launched, we can send a request to the application with curl to test the configuration. To execute the test, run the following command within a new interactive terminal:

Command to send a request to the container

After executing the curl command, we should receive a message from the application stating, Hello, Educative Learner! This is version 1.0.

Note: When sending traffic to the application, be sure to use a new interactive terminal to execute the curl command.

Try it yourself#

/
system
app
app.py
dockerfile
requirements.txt
LICENSE
Storing docker images in repositories

Conclusion#

The container registry serves an important role within GitOps by making container images available to orchestrators like Kubernetes. We can now proceed with running the container image stored on Docker Hub within a Kubernetes cluster.

Containerizing an Application

Provisioning a Kubernetes Cluster